package com.example.yunhailutong.zhongtan.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.GongYingEntity;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.squareup.picasso.Picasso;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yunhailutong on 2018/4/26.
 */

public class ActivityGongYing extends ActivityBase {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Override
    protected void addListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initActivity(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int contentViewById() {
        return R.layout.activity_baselistview;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        title.setText("供应信息");
        Map<String, String> params = new HashMap<>();
        params.put("index", "1");
        params.put("length","100");
        params.put("mmId","100");
        new PersionBiz().GetGongYingList(params);
        showWaitingDialog();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                Map<String, String> params = new HashMap<>();
                params.put("index", "1");
                params.put("length","100");
                new PersionBiz().GetGongYingList(params);
                //传入false表示刷新失败
                refreshlayout.finishRefresh();
            }
        });

    }
    private void initListView(GongYingEntity entity) {
        listview.setAdapter(new CommonAdapter<GongYingEntity.DataBean>(getActivity(), R.layout.main_item_base, entity.getData()) {
            @Override
            protected void convert(ViewHolder viewHolder, GongYingEntity.DataBean item, int position) {
                viewHolder.setText(R.id.title, item.getTitle());
                viewHolder.setText(R.id.text1, item.getPrice());
                viewHolder.setText(R.id.text2, item.getNum()+"吨");
                Picasso.with(mContext).load("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3094121875,3546519224&fm=27&gp=0.jpg").into((ImageView) viewHolder.getView(R.id.image));
                if(!item.getImg().equals("")){
                    String image = item.getImg();
                    String encode = null;
                    try {
                        encode = URLEncoder.encode(image, "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    String replace = encode.replace("%3A", ":").replace("%2F", "/");
                    Picasso.with(mContext).load(replace).into((ImageView) viewHolder.getView(R.id.image));
                }
            }
        });
    }

    @Subscribe
    public void log(OldsEvent.GongYingEvent msg) {
        dismissWaitingDialog();
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
            initListView((GongYingEntity) msg.obj);
        }
    }
}
